<script setup>
// 引入组件
import PageContainer from '@/components/PageContainer.vue'
import ChannelSelect from '@/components/ChannelSelect.vue'
import ArticleEidt from '@/components/ArticleEidt.vue'
// 引入icon
import { Delete, Edit } from '@element-plus/icons-vue'
// 引入vue相关的操作
import { ref, onMounted } from 'vue'
// 引入 api
import {
  //   articleInfoPutService,
  //   articleAddService,
  //   articleInfoService,
  articleDeleteService,
  artGetListService
} from '@/api/article' //  修改文章详情 新增文章发布文章 获取文章详情 删除文章 获取文章列表

import { formatTime } from '@/utils/format' //导入时间格式化方法
// 创建查询参数
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
const total = ref(0) // 创建文章总数
const articleList = ref([]) // 创建文章列表数据
onMounted(async () => {
  getArticleList()
})
const loading = ref(false) // 创建loading
//   获取文章列表
const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  console.log(res)

  total.value = res.total
  articleList.value = res.data
  loading.value = false
}
// 分页
const handleSizeChange = (size) => {
  params.value.pagenum = 1
  params.value.pagesize = size
  getArticleList()
}
const handleCurrentChange = (page) => {
  params.value.pagenum = page
  getArticleList()
}
const onSearch = () => {
  getArticleList()
}
const onReset = () => {
  params.value = {
    pagenum: 1,
    pagesize: 5,
    cate_id: '',
    state: ''
  }
  getArticleList()
}
const dialog = ref(null)
const onEditArticle = (row) => {
  dialog.value.open(row)
}
const onAddArticle = () => {
  dialog.value.open({})
}
const onDeleteArticle = async (row) => {
  await ElMessageBox.confirm('你确认删除该文章信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await articleDeleteService(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  await getArticleList()
}
// 添加修改成功
const onSuccess = (type) => {
  if (type === 'add') {
    // 如果是添加，需要跳转渲染最后一页，编辑直接渲染当前页
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">发布文章</el-button>
    </template>
    <!-- 搜索栏 -->
    <el-form inline class="search-form">
      <el-form-item label="文章分类：">
        <channel-select v-model="params.cate_id"></channel-select>
      </el-form-item>
      <el-form-item label="发布状态：">
        <el-select v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 文章列表栏 -->
    <el-table :data="articleList" style="width: 100%" v-loading="loading">
      <el-table-column label="文章标题" width="400">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDeleteArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 当前页数 -->
    <!-- 每页显示条数 -->
    <!-- 可选的每页条数 -->
    <!-- 大小 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[3, 5, 7]"
      size="small"
      layout="jumper,total, sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </page-container>
  <article-eidt ref="dialog" @success="onSuccess"></article-eidt>
</template>
<style lang="scss" scoped>
.search-form .el-input {
  --el-input-width: 220px;
}

.search-form .el-select {
  --el-select-width: 220px;
}
</style>
